package com.tamplus.cs.service;

import java.util.Locale;

import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.util.Assert;
import org.tempuri.CheckUpServiceSoapProxy;

import com.siebel.CustomUI.BGH_spc_spcAdd_spcContactProxy;
import com.siebel.CustomUI.BGH_spc_spcGet_spcContact_spcInfoProxy;
import com.siebel.CustomUI.BGH_spc_spcProduct_spcRecommendationProxy;
import com.siebel.CustomUI.BGH_spc_spcUpdate_spcAnswerProxy;
import com.siebel.CustomUI.BGH_spc_spcUpdate_spcProductProxy;
import com.tamplus.cs.constant.Constant;
import com.tamplus.cs.helper.MessageHelper;

public class BaseService implements InitializingBean{
	
	private ResourceBundleMessageSource message;
	
	public ResourceBundleMessageSource getMessage() {
		return message;
	}

	public void setMessage(ResourceBundleMessageSource message) {
		this.message = message;
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		Assert.notNull(message, "Must set message");
	}
	
	private String getEndPoint(String prop) throws Exception{		
		FacesContext context = FacesContext.getCurrentInstance();
		
		ExternalContext external = context.getExternalContext();
		HttpServletRequest request = (HttpServletRequest) external.getRequest();
		String user 	= (String)request.getSession().getAttribute(Constant.USERNAME);
		String password = (String)request.getSession().getAttribute(Constant.PASSWORD);
		String endPoint = MessageHelper.getLocaleString(context.getApplication().getMessageBundle(), prop, new Object[]{user, password}, Locale.US);
		
		return endPoint;
	}

	public CheckUpServiceSoapProxy getService(String prop) throws Exception{
//		FacesContext context = FacesContext.getCurrentInstance();
//		
//		String endPoint = MessageHelper.getLocaleString(context.getApplication().getMessageBundle(), prop, null, Locale.US);
		String endPoint = getEndPoint(prop);
		System.out.println("start call service...");
		CheckUpServiceSoapProxy service = new CheckUpServiceSoapProxy();
		System.out.println("service is..."+service);
		System.out.println("set end point..."+endPoint);
		service.setEndpoint(endPoint);
		System.out.println("set end point end..");
		return service;
	}

	public BGH_spc_spcAdd_spcContactProxy getAddContractService(String prop) throws Exception{
//		FacesContext context = FacesContext.getCurrentInstance();
//		
//		String endPoint = MessageHelper.getLocaleString(context.getApplication().getMessageBundle(), prop, null, Locale.US);
		String endPoint = getEndPoint(prop);
		System.out.println("start call service...");
		BGH_spc_spcAdd_spcContactProxy service = new BGH_spc_spcAdd_spcContactProxy();
		System.out.println("service is..."+service);
		System.out.println("set end point..."+endPoint);
		service.setEndpoint(endPoint);
		System.out.println("set end point end..");
		
		return service;
	}

	public BGH_spc_spcGet_spcContact_spcInfoProxy getGetContractService(String prop) throws Exception{
//		FacesContext context = FacesContext.getCurrentInstance();
//		
//		String endPoint = MessageHelper.getLocaleString(context.getApplication().getMessageBundle(), prop, null, Locale.US);
		String endPoint = getEndPoint(prop);
		System.out.println("start call service...");
		BGH_spc_spcGet_spcContact_spcInfoProxy service = new BGH_spc_spcGet_spcContact_spcInfoProxy();
		System.out.println("service is..."+service);
		System.out.println("set end point..."+endPoint);
		service.setEndpoint(endPoint);
		System.out.println("set end point end..");
		
		return service;
	}

	public BGH_spc_spcProduct_spcRecommendationProxy getProductRecService(String prop) throws Exception{
//		FacesContext context = FacesContext.getCurrentInstance();
//		
//		String endPoint = MessageHelper.getLocaleString(context.getApplication().getMessageBundle(), prop, null, Locale.US);
		String endPoint = getEndPoint(prop);
		System.out.println("start call service...");
		BGH_spc_spcProduct_spcRecommendationProxy service = new BGH_spc_spcProduct_spcRecommendationProxy();
		System.out.println("service is..."+service);
		System.out.println("set end point..."+endPoint);
		service.setEndpoint(endPoint);
		System.out.println("set end point end..");
		
		return service;
	}

	public BGH_spc_spcUpdate_spcAnswerProxy getUpdateAnswerService(String prop) throws Exception{
//		FacesContext context = FacesContext.getCurrentInstance();
//		
//		String endPoint = MessageHelper.getLocaleString(context.getApplication().getMessageBundle(), prop, null, Locale.US);
		String endPoint = getEndPoint(prop);
		System.out.println("start call service...");
		BGH_spc_spcUpdate_spcAnswerProxy service = new BGH_spc_spcUpdate_spcAnswerProxy();
		System.out.println("service is..."+service);
		System.out.println("set end point..."+endPoint);
		service.setEndpoint(endPoint);
		System.out.println("set end point end..");
		
		return service;
	}

	public BGH_spc_spcUpdate_spcProductProxy getUpdateProductService(String prop) throws Exception{
//		FacesContext context = FacesContext.getCurrentInstance();
//		
//		String endPoint = MessageHelper.getLocaleString(context.getApplication().getMessageBundle(), prop, null, Locale.US);
		String endPoint = getEndPoint(prop);
		System.out.println("start call service...");
		BGH_spc_spcUpdate_spcProductProxy service = new BGH_spc_spcUpdate_spcProductProxy();
		System.out.println("service is..."+service);
		System.out.println("set end point..."+endPoint);
		service.setEndpoint(endPoint);
		System.out.println("set end point end..");
		
		return service;
	}
}
